package com.itfallrain.controller;

import com.itfallrain.common.UserAuth;
import com.itfallrain.entity.Role;
import com.itfallrain.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    private AuthService authService;
    @UserAuth(2)
    @RequestMapping("/users")
    @ResponseBody
    public Object users(int page,int limit){
        Map<String,Object> map=new HashMap();
//        角色管理分页功能
        map.put("code",0);
        map.put("msg","");
        map.put("count",authService.getRoleCount());
        map.put("data",authService.getRoles(page,limit));
        return map;
    }
//    查询所有的角色，发送到前端
    @RequestMapping("/auths")
    @ResponseBody
    @UserAuth(3)
    public Object auths(int page, int limit) {
        Map<String, Object> map = new HashMap();
//        返回状态码
        map.put("code", 0);
        map.put("msg", "");
//        返回全部角色数量
        map.put("count", authService.getRoleCount());
//        返回角色权限
        map.put("data", authService.getRolesAuths(page, limit));
        return map;
    }
//    获取所有的权限
    @UserAuth(3)
    @RequestMapping("/allauths")
    @ResponseBody
    public Object get_all_auths(){
        return authService.selectAllAuth();
    }
//    修改权限
    @UserAuth(3)
    @RequestMapping("/edit_auths")
    @ResponseBody
    public Object edit_auths(Integer role_id, Integer[] auths) {
        Map<String, Object> map = new HashMap();

        if (authService.editRoleAuths(role_id, auths)) {
            map.put("code", 1);
        } else {
            map.put("code", 0);
            map.put("msg", "权限修改失败！");
        }
        return map;
    }















    //    新增
    @RequestMapping("/add")
    @ResponseBody
    public Object AddAuth(String name){

        Map<String,Object> map=new HashMap<String, Object>();
        int i = authService.selectadd(name);
        if(i==1){
            map.put("code",1);
            map.put("msg","修改成功！");

        }else {
            map.put("name",name);
            map.put("code",0);
            map.put("msg","修改失败");
        }
        return map;
    }
//    删除用户
    @RequestMapping("/del")
    @ResponseBody
    public Object DelAuth(int id){
        Map<String,Object> map=new HashMap<String, Object>();
        int i = authService.selectdel(id);
        if(i==1){
            map.put("code",1);
            map.put("msg","删除成功！");

        }else {
            map.put("id",id);
            map.put("code",0);
            map.put("msg","删除失败");
        }
        return map;
    }
//    修改用户
    @RequestMapping("/edit")
    @ResponseBody
    public Object EditAuth(String name){
        Map<String,Object> map=new HashMap<String, Object>();
        int i = authService.selectadd(name);
        if(i==1){
            map.put("code",1);
            map.put("msg","修改成功!！");

        }else {
            map.put("name",name);
            map.put("code",0);
            map.put("msg","修改失败");
        }
        return map;
    }


}
